﻿var express = require('express');
var router = new express.Router();
var Users = require('../../Module/User');
var dbQryCtl = require('../../Controllers/dbQueryCtller');

router.get('/', function (req, res) {
    if (req.session.user) {
        res.redirect("/user");
    } else {
        res.redirect("/login");
    }
});

router.get('/login', function (req, res) {
    res.render('index', { title: '加班登记', year: new Date().getFullYear() });
}).post('/login', function (req, res) {
    var login_account = req.body.user;
    var login_psw = req.body.psw;
    Users.findByAccount(login_account, function (err, user) {
        if (err != null) {
            req.session.user = undefined;
            res.send(JSON.stringify({ data: err, code: 901 }));
            res.end();
        } else {
            if (user != null) {
                user.comparePassword(login_psw, function (err, ismatch) {
                    if (err) {
                        req.session.user = undefined;
                        res.send(JSON.stringify({ data: err, code: 901 }));
                        res.end();
                    } else if (ismatch) {
                        Users.findOneAndUpdate(
                            { account: user.account },
                            {
                                online: true,
                                lst_login_time: Date.now()
                            },
                            function (err, data) {
                                if (err) {
                                    console.error(err);
                                } else {
                                    console.log(data);
                                }
                            }
                        );
                        req.session.user = user;
                        res.send(JSON.stringify({ user: user, url: "/user", data: "登入成功", code: 200 }));
                        res.end
                    } else {
                        req.session.user = undefined;
                        res.send(JSON.stringify({ data: "密码错误!",code:202 }));
                        res.end();
                    }
                });
            } else {
                req.session.user = undefined;
                res.send(JSON.stringify({ data: "用户名不存在!", code:201 }));
                res.end();
            }
        }
    });
});

router.get('/register', function (req, res) {
    res.render('Register', { title: '用户注册'});
}).post('/register', function (req, res) {
    var account = req.body.user;
    var psw = req.body.psw;
    var emp_name = req.body.emp_name;
    var emp_no = req.body.emp_no;
    var dpt_name = req.body.dpt_name;
    var position = req.body.position;
    Users.findByAccount(account, function (err, user) {
        if (err != null) {
            res.send(JSON.stringify({ data: err, code: 901 }));
            res.end();
        } else if (user != null) {
            res.send(JSON.stringify({ data: "用户名已存在", code: 203 }));
            res.end();
        } else {
            Users.create({
                account: account,
                employee_no: emp_no,
                employee_name: emp_name,
                psw: psw,
                department_name: dpt_name,
                position: position,
                online: true
            }, function (err, data) {
                if (err === null) {
                    req.session.user = data;
                    res.send(JSON.stringify({user:data, url: "/user", data: "注册成功", code: 200 }));
                    res.end
                } else {
                    req.session.user = undefined;
                    res.send(JSON.stringify({ data: err, code: 901 }));
                    res.end();
                }
            });            
        }
    });
});

router.get('/logout', function (req, res) {
    if (req.session.user) {
        Users.findOneAndUpdate({ account: req.session.user.account }, {
            online: false,
            lst_logout_time: Date.now()
        }, function (err, data) {
            if (err) {
                console.error(err);
                res.render('index', { title: '加班登记', year: new Date().getFullYear() });
            } else {
                console.log(data);
                req.session.user = undefined;
                res.render('index', { title: '加班登记', year: new Date().getFullYear() });
            }
        });
    }
    else {
        res.render('index', { title: '加班登记', year: new Date().getFullYear() });
    }    
});

router.post('/querydb', function (req, res) {
    var queryID = req.body.queryID;
    dbQryCtl.dbQuery(queryID, function (err, data) {
        if (err) { res.end(JSON.stringify({ data: err, code: 901 })); }
        else { res.end(JSON.stringify({ data: data, code: 200 })); }
    });
});
module.exports = router;